<?php
class appRain_Base_Modules_Content extends appRain_Base_Modules_Utility
{
	private $content = "";
	const CACHE_PAGE_PRE_FIX = '__page__';

	public function blockFormated($content="")
	{
		$this->content = $content;

		$this->fragmentize()
			 ->Processing()
			 ->finaize();

	    return $this->content;

	}
	private function fragmentize()
	{
		$this->content = preg_split("[{{|}}]",$this->content);

		return $this;
	}
	private function Processing()
	{
		foreach($this->content as $key=>$row){if($key%2==1 && trim($row)){
			$row = $this->splitInLine($row);
			switch($row['type'])
			{
				case 'staticpage' : $this->replaceStaticPage($key,$row);
									break;
			}

		}}

		return $this;
	}
	private function splitInLine($row)
	{
		$row = preg_split("[=|;]",$row);
		$tmp=array();
		for($kk=0;$kk<sizeof($row);$kk+= 2)
		{
			$row[$kk+1] = isset($row[$kk+1]) ? $row[$kk+1]  : "";
			$row[$kk] = isset($row[$kk]) ? $row[$kk]  : "";
			$tmp[$row[$kk]]=trim($row[$kk+1]);
		}
		return $tmp;
	}
	private function replaceStaticPage($key,$row)
	{
		$data = App::PageManager()->getData($row['pagename']);
		$this->content[$key] =isset($data['content'])?$data['content']:"";
	}
	private function finaize()
	{
		$this->content = implode("",$this->content);
	}

	private function readCache($code="")
    {
        return App::Module('Cache')->Read($code);
    }

    private function createCache($code,$content)
    {
        App::Module('Cache')->Write($code,$content);
        return $this;
    }

    private function hasCache($code="")
    {
       return App::Module('Cache')->exists($code);
    }
}